package com.tcwlkj.uuService.NetWork;

import android.content.Context;
import android.util.Log;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.utils.NetUtils;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.Tools;
import com.tcwlkj.uuService.utils.WaitDialog;
import com.tcwlkj.uuService.utils.WaitDialogUtil;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/8 0008.
 */

public class RetrofitUtils {

    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(HttpIp.Public)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private static Retrofit new_retrofit = new Retrofit.Builder()
            .baseUrl(HttpIp.chat)
            .client(genericClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

//    private static Retrofit kefu_retrofit = new Retrofit.Builder()
//            .baseUrl(HttpIp.chat)
//            .client(genericClient())
//            .addConverterFactory(GsonConverterFactory.create())
//            .build();

    private static Retrofit upload_retrofit = new Retrofit.Builder()
            .baseUrl(HttpIp.image)
            .client(genericClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    private static Retrofit baidu_retrofit = new Retrofit.Builder()
            .baseUrl("http://api.map.baidu.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    /**
     *老接口
     */
    public static RequestServes getRequestService() {
        RequestServes requestServes = retrofit.create(RequestServes.class);
        return requestServes;
    }
    /**
     *通用
     */
    public static RequestServes getRequest(Context context,boolean flag) {
        if(context!=null&&flag){
            WaitDialogUtil.getInstance().showDialog(context);
        }
        RequestServes requestServes = new_retrofit.create(RequestServes.class);
        return requestServes;
    }

    /**
     *上传图片
     */
    public static RequestServes getUploadRequest(Context context,boolean flag) {
        if(context!=null&&flag){
            WaitDialogUtil.getInstance().showDialog(context);
        }
        RequestServes requestServes = upload_retrofit.create(RequestServes.class);
        return requestServes;
    }

    /**
     *获取openID
     */
    public static RequestServes getOpenidRequest() {
        RequestServes requestServes = new_retrofit.create(RequestServes.class);
        return requestServes;
    }

    /**
     *百度接口
     */
    public static RequestServes getBaiduRequest() {
        RequestServes requestServes = baidu_retrofit.create(RequestServes.class);
        return requestServes;
    }

    /**
//     *在线客服
//     */
//    public static RequestServes getServiceRequest(){
//        RequestServes requestServes = kefu_retrofit.create(RequestServes.class);
//        return requestServes;
//    }


    public static OkHttpClient genericClient() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request oldRequest = chain.request();
                        Response  response=chain.proceed(oldRequest);
                        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                                .newBuilder()
                                .scheme(oldRequest.url().scheme())
                                .host(oldRequest.url().host())
                                .addQueryParameter("openid", PreferencesUtils.getString(MyApp.getContext(),"openid"));//添加openid公共参数
                            // 新的请求
                            String  timestamp=System.currentTimeMillis()+"";
                            String key= Tools.md5("CityWide.Auth-"+ timestamp+"-TimeStampCityWide.Auth-Pass-Key");
                            Request newRequest = oldRequest.newBuilder()
                                    .method(oldRequest.method(), oldRequest.body())
                                    .url(authorizedUrlBuilder.build())
                                    .addHeader("Auth-Pass-Key",key)//添加key请求头
                                    .addHeader("Auth-Time-Stamp", timestamp)//添加时间戳头
                                    .build();
                            return chain.proceed(newRequest);
                    }
                })
                .build();
        return httpClient;
    }

}
